package org.microblog.dao.impl;

import org.microblog.dao.BaseDao;
import org.microblog.dao.CommentDao;
import org.microblog.entity.Comment;
import org.microblog.util.DatabaseUtil;
import org.microblog.util.DateUtil;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CommentDaoImpl extends BaseDao implements CommentDao {
    public CommentDaoImpl(Connection conn){
        super(conn);
    }
    @Override
    public int getCommentCountByBid(int bid) throws SQLException{
        ResultSet rs = null;
        //获取微博评价数
        String sql = "select count(*) from comment where bid = ?" ;
        int count = 0;

        try{
            rs = this.executeQuery(sql, bid);
            while (rs.next()) {
                count = rs.getInt("count(*)");
            }
        }catch (SQLException e) {
            e.printStackTrace();
            throw e;
        } finally {
            DatabaseUtil.closeAll(null, null, rs);
        }
        return count;
    }

    @Override
    public List<Comment> getCommentsByBid(int bid) throws SQLException{
        List<Comment> list = new ArrayList<Comment>();
        ResultSet rs = null;
        //根据微博ID查询所有评论
        String sql = "select bid, content, time, nickname, image from comment inner join user where comment.uid = user.id and bid = ? order by time desc";

        try{
            rs = this.executeQuery(sql,bid);
            Comment comment = null;
            while (rs.next()) {
                comment = new Comment();
                comment.setBid(rs.getInt("bid"));
                comment.setContent(rs.getString("content"));
                comment.setTime(rs.getTimestamp("time"));
                comment.setuNickname(rs.getString("nickname"));
                comment.setuImage(rs.getString("image"));
                list.add(comment);
            }
        }catch (SQLException e) {
            e.printStackTrace();
            throw e;
        } finally {
            DatabaseUtil.closeAll(null, null, rs);
        }
        return list;
    }

    @Override
    public int addComment(Comment comment) throws SQLException{
        String now = DateUtil.now();
        String sql = "insert into comment(uid,bid,content,time) values(?,?,?,?)";
        int result = 0;
        try {
            result = this.executeUpdate(sql, comment.getUid(),comment.getBid(),comment.getContent(),now);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
        return result;
    }

    @Override
    public int deleteCommentsByBid(int bid) throws SQLException {
        String sql = "delete from comment where bid = ?";
        int result = 0;
        try{
            result = executeUpdate(sql, bid);
        }catch(Exception e){
            e.printStackTrace();
            throw e;
        }
        return result;
    }

    @Override
    public List<Comment> getCommentsByPage(int bid, int pageNo, int pageSize) throws SQLException {
        List<Comment> list = new ArrayList<Comment>();
        ResultSet rs = null;
        //根据微博ID查询所有评论
        String sql = "select bid, content, time, nickname, image from comment inner join user where comment.uid = user.id and bid = ? order by time desc LIMIT ?, ?";

        try{
            rs = this.executeQuery(sql,bid, (pageNo - 1) * pageSize,pageSize);
            Comment comment = null;
            while (rs.next()) {
                comment = new Comment();
                comment.setBid(rs.getInt("bid"));
                comment.setContent(rs.getString("content"));
                comment.setTime(rs.getTimestamp("time"));
                comment.setuNickname(rs.getString("nickname"));
                comment.setuImage(rs.getString("image"));
                list.add(comment);
            }
        }catch (SQLException e) {
            e.printStackTrace();
            throw e;
        } finally {
            DatabaseUtil.closeAll(null, null, rs);
        }
        return list;
    }
}
